<template>
    <div>
        <div class="editor-title">
            <span class="editor-title__name">{{ data.text }}</span>
            <div class="editor-title__sub-text">用于循环播放展示一组消息通知。</div>
        </div>
        <div class="img-btn-con" style="padding: 4px 16px 20px">
            <div class="cls-between" style="padding-bottom: 1rem; margin-top: 28px">
                <div class="label-con" style="flex: 1">公告样式</div>
                <div class="cls-between">
                    <div class="item-con" :class="data.style.notice_type == 1 ? 'item-con-active' : ''" @click="data.style.notice_type = 1">纯文本</div>
                    <!-- <div class="item-con" :class="data.style.notice_type == 2 ? 'item-con-active' : ''" @click="data.style.notice_type = 2">文章分类</div> -->
                </div>
            </div>
            <div class="editor-two__item" v-if="data.style.notice_type == 1">
                <div class="editor-two__item-head">公告内容</div>
                <el-input v-model="data.data.data" placeholder="输入公告通知内容" clearable />
            </div>
            <div class="cls-between" style="padding-bottom: 1rem" v-if="data.style.notice_type == 2">
                <div class="label-con" style="flex: 1">选择文章分类</div>
                <div class="cls-between">
                    <el-select v-model="data.data.cate_id" placeholder="请选择文章分类" clearable @change="changeCate">
                        <el-option v-for="item in newsCates" :key="item.cate_id" :label="item.title" :value="item.cate_id" />
                    </el-select>
                </div>
            </div>
            <div class="cls-between" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">公告左侧图标</div>
                <div class="cls-between">
                    <div class="item-con" :class="data.style.type == 1 ? 'item-con-active' : ''" @click="clickStyle(1)">标准</div>
                    <div class="item-con" :class="data.style.type == 2 ? 'item-con-active' : ''" @click="clickStyle(2)">自定义</div>
                </div>
            </div>
            <div style="padding-bottom: 1rem" v-if="data.style.type == 2">
                <div class="label-con" style="flex: 1; padding-bottom: 1rem; width: 100%">
                    自定义左侧图标<span class="label-con__span">（建议：109x25像素）</span>
                </div>
                <div>
					<el-upload
					    ref="uploadRef"
					    :limit="10"
					    class="better-img"
						:show-file-list="false"
					    :action="actionImgUrl()"
					    :http-request="uploadImgs"
					>
					    <div style="position: relative; cursor: pointer">
					        <img :src="data.data.custom_icon" style="width: 60px; height: 60px" v-if="data.data.custom_icon" />
					        <div class="cls-center" style="width: 60px; height: 60px; background-color: #f6f6f6" v-else>
					            <el-icon size="20px" color="#999">
					                <Plus />
					            </el-icon>
					        </div>
					        <div class="tip-con">{{ data.data.custom_icon ? "更换图片" : "添加图片" }}</div>
					    </div>
					</el-upload>
                    
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">背景颜色</div>
                <div>
                    <el-color-picker v-model="data.style.background" @change="color_change('background', '#fffbe8')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">文字颜色</div>
                <div>
                    <el-color-picker v-model="data.style.color" @change="color_change('color', '#ed6a0c')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">右侧链接箭头</div>
                <div>
                    <el-switch v-model="data.style.arrow_switch" width="60px" size="large" inline-prompt active-text="显示" inactive-text="隐藏" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">公告倒角</div>
                <div style="flex: 1">{{ data.rpxStyle["border-radius"] == 0 ? "直角" : "圆角" }}</div>
                <div class="cls-flex">
                    <el-button
                        :icon="Picture"
                        :type="data.rpxStyle['border-radius'] == 0 ? 'primary' : 'default'"
                        plain
                        @click="data.rpxStyle['border-radius'] = 0"
                    />
                    <el-button
                        :icon="PictureRounded"
                        :type="data.rpxStyle['border-radius'] != 0 ? 'primary' : 'default'"
                        plain
                        @click="data.rpxStyle['border-radius'] = 5"
                    />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">圆角距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['border-radius']" :max="max" @change="radius_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['border-radius']" @input="radius_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">页面边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-left']" :max="max" @change="padding_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-left']" @input="padding_change" />
                </div>
            </div>
            <!-- <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">容器高度</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle.height" :max="400" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle.height" />
                </div>
            </div> -->
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">上边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-top']" :min="0" :max="300" @change="mtt_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-top']" @input="mtt_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">下边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-bottom']" :min="0" :max="300" @change="mtb_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-bottom']" @input="mtb_change" />
                </div>
            </div>
        </div>
    </div>

    <!-- 图片库 -->
    <picture-library :show="showImg" @close="showImg = false" @select="selectImg" :select_multiple="false"> </picture-library>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted } from "vue"
import { Picture, PictureRounded, Plus } from "@element-plus/icons-vue"
import PictureLibrary from "@components/PictureLibrary.vue"
import UploadImg from "@/mixins/upload-img";
const uploadRef = ref(null);
	const {
	    succList,
	    imgLoading,
	    handlePreview,
	    actionImgUrl,
	    uploadImgAssembly,
	} = UploadImg();

const { proxy } = getCurrentInstance()
const max = 30 // 最大值，适用于内外边距和圆角

/**
 * 通知公告
 * @property {Object} pageData  页面数据
 */
const props = defineProps({
    data: {
        type: Object,
        default: {
            id: 6,
            type_name: "notice",
            text: "公告通知",
            style: {
                notice_type: 1, // 公告样式，1是纯文本，2是文章列表
                type: 1, // 公告左侧图标，1是标准，2是自定义图标
                background: "#fffbe8", // 滚动条背景
                color: "#ed6a0c", // 文本颜色
            },
            data: {
                data: "请填写内容，内容过长时，将会在手机上滚动显示",
                custom_icon: "", // 自定义图标
            },
        },
    },
})

onMounted(() => {
    // getCate()
})

/**
 * 分类操作
 * @param {Array} newsCates  分类数组 data.style.arrow_switch
 */
const changeCate = (val) => {
    let indexs = newsCates.value.findIndex(item => item.cate_id == val)
    indexs > - 1 && (props.data.data.cate_name = newsCates.value[indexs].title)
    proxy.$axios1
        .get("/api/article/index", {
            params: {
                cate_id: val,
            },
        })
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                props.data.data.reqData = result.slice(0, 3);
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            dataObj.pageLoading = false
        })
}
const newsCates = ref([])
function getCate() {
    proxy.$axios1
        .get("/api/article/cate")
        .then((res) => {
            if (res.data.code == 0) {
                newsCates.value = res.data.data
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            dataObj.pageLoading = false
        })
}

// 公告左侧样式
const clickStyle = (type) => {
    props.data.style.type = type
}

// 图标选择
const showImg = ref(false)
const selectImg = (val) => {
    showImg.value = false
    props.data.data.custom_icon = proxy.imgUrl + val[0].photo
}

function uploadImgs(e){
	console.log("imgs",e)
	uploadImgAssembly(e,res=>{
		props.data.data.custom_icon =res.url
		
	})
	
}

// 页面边距变化
const padding_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["margin-left"] = props.data.rpxStyle["margin-right"] = Number(val)
}

// 圆角变化
const radius_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["border-radius"] = Number(val)
}

// 上下边距
const mtt_change = (val) => {
    props.data.rpxStyle["margin-top"] = Number(val)
}
const mtb_change = (val) => {
    props.data.rpxStyle["margin-bottom"] = Number(val)
}

/**
 * 当点击清空颜色时，恢复默认颜色
 * @param {*} key 要修改的颜色字段
 * @param {*} color 默认颜色， 当key值为null时，恢复默认颜色
 */
const color_change = (key, color) => {
    if (!props.data.style[key]) {
        props.data.style[key] = color
    }
}
</script>

<style lang="less" scoped>
.editor-two__item {
    margin-bottom: 20px;
}

.editor-two__item-head {
    color: #969799;
    padding-bottom: 10px;
}

.item-con {
    color: #323232;
    padding: 7px 14px;
    border: 1px solid #ebedf0;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
}

.item-con-active {
    color: #4b93fa !important;
    background-color: #edf4ff !important;
    border: 1px solid #4b93fa;
}

.el-button + .el-button {
    margin-left: 0 !important;
}

.el-button {
    border-radius: 0 !important;
}

.label-con {
    flex: none;
    width: 82px;
    font-size: 14px;
    padding-right: 10px;
    color: #969799;

    .label-con__span {
        font-size: 13px;
        color: #999;
    }
}

.tip-con {
    font-size: 12px;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 2px;
    width: 60px;
    text-align: center;
}
</style>
